package com.mall.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;



@Data
//@Component
@Configuration
//@ConfigurationProperties(prefix = "redis")
@EnableCaching
public class RedisConfig {

    private String host;
    private Integer port;
    private Integer database;
    private Integer poolMaxActive;
    private Integer poolMaxWait;
    private Integer poolMaxIdle;
    private Integer poolMinIdle;
    private Integer timeout;


//    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        // 创建一个JedisConnectionFactory对象
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        // 设置Redis主机名
        jedisConnectionFactory.setHostName(host);
        // 设置Redis端口
        jedisConnectionFactory.setPort(port);
        // 设置数据库索引号
        jedisConnectionFactory.setDatabase(database);
        // 设置连接超时时间
        jedisConnectionFactory.setTimeout(timeout);
        // 创建一个JedisPoolConfig对象
//        JedisPoolConfig poolConfig = new JedisPoolConfig();
//        // 设置最大连接数
//        poolConfig.setMaxTotal(poolMaxActive);
//        // 设置最大等待时间
//        poolConfig.setMaxWaitMillis(poolMaxWait);
//        // 设置最大空闲连接数
//        poolConfig.setMaxIdle(poolMaxIdle);
//        // 设置最小空闲连接数
//        poolConfig.setMinIdle(poolMinIdle);
//        // 设置JedisPoolConfig对象为JedisConnectionFactory的配置
//        jedisConnectionFactory.setPoolConfig(poolConfig);
        // 返回JedisConnectionFactory对象
        return jedisConnectionFactory;
    }

//    @Bean
//    public RedisTemplate<String, Object> redisTemplate() {
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//// string类型key序列器
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        // string类型value序列器
//        redisTemplate.setValueSerializer(redisserializer());
//// Hash类型 key序列器
//        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//        // Hash类型value序列器
//        redisTemplate.setHashValueSerializer(redisserializer());
////把连接工厂放进去
//        redisTemplate.setConnectionFactory(redisConnectionFactory());
//        return redisTemplate;
//
//    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// string类型key序列器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // string类型value序列器
        redisTemplate.setValueSerializer(redisserializer());
// Hash类型 key序列器
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // Hash类型value序列器
        redisTemplate.setHashValueSerializer(redisserializer());
//把连接工厂放进去
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;

    }

    @Bean
    public RedisSerializer redisserializer() {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        return jackson2JsonRedisSerializer;

    }
}